﻿package mortal.game.view.platform
{
    import Message.Game.*;
    import Message.Public.*;
    import __AS3__.vec.*;
    import flash.events.*;
    import mortal.common.global.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class PlatformVIPGiftsController extends Controller
    {
        private var _module:PlatformVIPGiftsModule;

        public function PlatformVIPGiftsController()
        {
            PlatformVIPGiftsModule.platformData = PlatformVIPGiftsUtil.getPanelData();
            return;
        }// end function

        protected function checkIsShowIcon(param1:Object = null) : void
        {
            if (ParamsConst.instance.platformVipUpgradeUrl && this.isShow())
            {
                PlatformVIPGiftIcon.instance.showIconByBtnName(PlatformVIPGiftsUtil.getIconResName());
            }
            else
            {
                if (PlatformVIPGiftIcon.isViewShow())
                {
                    PlatformVIPGiftIcon.instance.hide();
                }
                if (isViewShow)
                {
                    view.hide();
                }
            }
            return;
        }// end function

        private function checkIsReward() : void
        {
            var _loc_2:Vector.<TPlatformGift> = null;
            var _loc_3:int = 0;
            var _loc_1:Boolean = false;
            if (PlatformConst.isThunder())
            {
                _loc_1 = PlatformVIPGiftsUtil.hasThunderGift();
            }
            else
            {
                _loc_2 = PlatformGiftConfig.instance.vipGiftVec;
                _loc_3 = 0;
                while (_loc_3 < _loc_2.length)
                {
                    
                    if (PlatformVIPGiftsUtil.getReceiveTypeByTPlatformGift(_loc_2[_loc_3]) == PlatformVIPGiftGroup.CanReceive)
                    {
                        _loc_1 = true;
                        break;
                    }
                    _loc_3++;
                }
            }
            if (!PlatformVIPGiftIcon.isViewShow())
            {
                return;
            }
            if (_loc_1)
            {
                PlatformVIPGiftIcon.instance.playEffect();
            }
            else
            {
                PlatformVIPGiftIcon.instance.stopEffect();
            }
            return;
        }// end function

        override protected function initView() : IView
        {
            if (this._module == null)
            {
                this._module = new PlatformVIPGiftsModule();
                this._module.addEventListener(WindowEvent.SHOW, this.onShow);
                this._module.addEventListener(WindowEvent.CLOSE, this.onClose);
            }
            return this._module;
        }// end function

        protected function onShow(event:WindowEvent) : void
        {
            return;
        }// end function

        protected function onClose(event:WindowEvent) : void
        {
            return;
        }// end function

        override protected function initServer() : void
        {
            Dispatcher.addEventListener(EventName.PlatformVIPGiftShow, this.onPlatformViewShow);
            NetDispatcher.addCmdListener(ServerCommand.PlatformGiftInfoUpdate, this.onGiftInfoUpdate);
            NetDispatcher.addCmdListener(ServerCommand.PlatformVipInfoUpdate, this.onPlatformInfoUpdate);
            Dispatcher.addEventListener(EventName.PlatformRequestVIPGift, this.onRequestGift);
            return;
        }// end function

        private function onRequestGift(event:DataEvent) : void
        {
            GameProxy.Platform.getPlatformGift(int(event.data), this.onGetPlatformVIPGiftSuccess);
            return;
        }// end function

        private function onGetPlatformVIPGiftSuccess(param1:AMI_IRole_getPlatFormGift) : void
        {
            Cache.instance.platform.platformGiftInfo.push(int(param1.userObject));
            this.updateShow();
            return;
        }// end function

        private function isShow() : Boolean
        {
            if (PlatformConst.isThunder())
            {
                return !PlatformVIPGiftsUtil.isCompThunder();
            }
            var _loc_1:Boolean = false;
            var _loc_2:* = PlatformGiftConfig.instance.vipGiftVec;
            var _loc_3:int = 0;
            while (_loc_3 < _loc_2.length)
            {
                
                if (PlatformVIPGiftsUtil.getReceiveTypeByTPlatformGift(_loc_2[_loc_3]) != PlatformVIPGiftGroup.HasReceive)
                {
                    _loc_1 = true;
                }
                _loc_3++;
            }
            return _loc_1;
        }// end function

        private function updateShow() : void
        {
            if (isViewShow)
            {
                this._module.refresh();
            }
            if (!this.isShow())
            {
                if (isViewShow)
                {
                    this._module.hide();
                }
            }
            this.checkIsShowIcon();
            this.checkIsReward();
            return;
        }// end function

        private function onGiftInfoUpdate(param1:Object) : void
        {
            Cache.instance.platform.platformGiftInfo = (param1 as SPublicSeqInt).publicSeqInt;
            this.checkIsShowIcon();
            return;
        }// end function

        private function onPlatformInfoUpdate(param1:Object) : void
        {
            Cache.instance.platform.proxyVipInfo = param1 as SProxyInfo;
            this.checkIsReward();
            return;
        }// end function

        private function onPlatformViewShow(event:Event) : void
        {
            if (!isViewShow)
            {
                view.show();
            }
            else
            {
                view.hide();
            }
            return;
        }// end function

    }
}
